package com.study.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    //   简易交换机和队列
    public static final String SIMPLE_FANOUT_EXCHANGE = "simple.fanout.exchange";
    public static final String SIMPLE_FANOUT_QUEUE = "simple.fanout.queue";
    /**
     * 定义交换机
     *
     * @return Exchange
     */
    @Bean
    public FanoutExchange simpleExchange() {
        return new FanoutExchange(SIMPLE_FANOUT_EXCHANGE);
    }

    /**
     * 定义队列
     *
     * @return Queue
     */
    @Bean
    public Queue simpleQueue() {
        return new Queue(SIMPLE_FANOUT_QUEUE);
    }

    /**
     * 绑定交换机和队列
     * @return Binding
     */
    @Bean
    public Binding simpleBinding() {
        return BindingBuilder.bind(simpleQueue()).to(simpleExchange());
    }

    /**
     * json序列化工具使用jackson代替jdk
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
